package com.v1.weibo4j;

import weibo4j.Weibo;
import weibo4j.http.Response;
import weibo4j.model.PostParameter;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import weibo4j.util.WeiboConfig;

public class Shorturl extends Weibo {
	public String shorturl(String url) throws WeiboException {
		Response res = client.get(WeiboConfig.getValue("baseURL") + "short_url/shorten.json",
				new PostParameter[] { new PostParameter("url_long", url) });
		JSONObject json = res.asJSONObject();
		try {
			JSONArray urls = json.getJSONArray("urls");
			JSONObject url0 = urls.getJSONObject(0);
			String url_short = url0.getString("url_short");
			return url_short;
		} catch (JSONException je) {
			throw new WeiboException(je.getMessage() + ":" + json.toString(), je);
		}
	}
}
